<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\workerman\controller\WorkerEvents;
use GatewayWorker\Lib\Gateway;
use think\Db;

/**
 * 示例接口
 */
class Preside extends Api
{

    //如果$noNeedLogin为空表示所有接口都需要登录才能请求
    //如果$noNeedRight为空表示所有接口都需要验证权限才能请求
    //如果接口已经设置无需登录,那也就无需鉴权了
    //
    // 无需登录的接口,*表示全部
//    protected $noNeedLogin = ['test', 'test1'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    public function add()
    {
        $msg_data=$this->request->param();
        if (!isset($msg_data['number']) || strlen(strval($msg_data['number'])) <= 0) {
            $this->error('玩家人数有误');
        }
        if (!isset($msg_data['role_list']) || strlen(strval($msg_data['role_list'])) <= 0) {
            $this->error('角色选择有误');
        }

        if (!isset($msg_data['play_id']) || strlen(strval($msg_data['play_id'])) <= 0) {
            $this->error('玩法选择有误');
        }

        //主持人开始对局
        $role=explode(',',$msg_data['role_list']);
        if(count($role)!=$msg_data['number']){
            $this->error('人数与角色数量不同');
        }
        $group=WorkerEvents::getGroup();
        $id=Db::name('room')->insertGetId([
            'user_id'=>$this->auth->id,
            'room_group'=>$group,
            'play_id'=>$msg_data['play_id'],
            'number'=> $msg_data['number'],
            'role_list'=>$msg_data['role_list'],
            'createtime'=>time(),
        ]);
        $client_id=Gateway::getClientIdByUid($this->auth->id);
        if($client_id){
            foreach($client_id as $k=>$v){
                Gateway::joinGroup($v, $id);
            }
        }
        $this->success('',['id'=>$id,'room_group'=>$group]);
    }

    public function room()
    {
        $param=$this->request->param();
        $data=Db::name('game')->alias('g')
            ->join('user u','g.user_id=u.id','left')
            ->join('role r','g.role_id=r.id','left')
            ->where('g.room_id',$param['room_id'])
            ->field('g.*,u.nickname,u.avatar,r.image,r.name as role_name,r.type')
            ->select();
        foreach($data as $k=>$v){
            if($v['type']=='2'){
                $v['friend']=true;
            }else{
                if($v['role_name']=='模仿鹅'){
                    $v['friend']=true;
                }else{
                    $v['friend']=false;
                }
            }
            $data[$k]=$v;
        }
        $indexModel=new \app\api\model\Index();
        $data=$indexModel->cdnData($data);
        $this->success('',$data);
    }

    public function getRoom()
    {
        $param=$this->request->param();
        $data=Db::name('room')->where('room_group',$param['room_group'])->where('status','neq','3')->find();
        if(!$data){
            $this->error('房间号有误');
        }
        $role=explode(',',$data['role_list']);
        $ro=[];
        foreach($role as $k=>$v){
            $rol=Db::name('role')->where('id',$v)->find();
            $rol['image']=cdnurl($rol['image'],true);
            $ro[]=$rol;
        }
        $data['role']=$ro;
        $this->success('',$data);
    }

    public function play()
    {
        $user=Db::name('user')->where('id',$this->auth->id)->find();
        $data=Db::name('play')->where('shop_id',$user['shop_id'])->where('status','1')->select();
        $indexModel=new \app\api\model\Index();
        $data=$indexModel->cdnData($data);
        $this->success('',$data);
    }

    public function check()
    {
        $param=$this->request->param();
        if (!isset($param['room_id']) || intval($param['room_id']) <= 0) {
            $this->error('room_id有误');
        }
        $shop_id=Db::name('room')->alias('r')
            ->join('user u','r.user_id=u.id','left')
            ->where('r.id',$param['room_id'])
            ->value('shop_id');
        $data=Db::name('play')->where('shop_id',$shop_id)->where('status','1')->select();
        $indexModel=new \app\api\model\Index();
        $data=$indexModel->cdnData($data);
        $this->success('',$data);

    }

    public function shop()
    {
        $data=Db::name('shop')->alias('s')
            ->join('time t','s.id=t.shop_id','left')
            ->where('t.user_id',$this->auth->id)
            ->where('s.status','1')
            ->select();
        $this->success('',$data);
    }

    public function exchange()
    {
        $param=$this->request->param();
        if (!isset($param['shop_id']) || strlen(strval($param['shop_id'])) <= 0) {
            $this->error('店铺有误');
        }

        if (!isset($param['code']) || strlen(strval($param['code'])) <= 0) {
            $this->error('兑换码有误');
        }

        $data=Db::name('exchange')->where('shop_id',$param['shop_id'])
            ->where('status','2')
            ->where('code',$param['code'])
            ->find();
        if($data){
            $res=Db::name('time')->where('shop_id',$param['shop_id'])
                ->where('user_id',$this->auth->id)
                ->setInc('minute',$data['minute']);
            Db::name('exchange')->where('shop_id',$param['shop_id'])
                ->where('status','2')
                ->where('code',$param['code'])
                ->update(['status'=>'1','user_id'=>$this->auth->id]);
            $this->success('');
        }
        $this->error('兑换码有误');
    }

    public function editInfo(){
        $param=$this->request->param();
        if(!isset($param['nickname'])||strlen(strval($param['nickname']))<=0){
            $this->error('昵称有误');
        }
        if(!isset($param['avatar'])||strlen(strval($param['avatar']))<=0){
            $this->error('头像有误');
        }
        $res=Db::name('user')->where('id',$this->auth->id)->update($param);
        if($res){
            $this->success('成功');
        }else{
            $this->error('失败');
        }
    }


}
